/*
 * Copyright 2013 meichenhui
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.zet.tool.pluginanalyzer;

import java.io.Console;
import java.util.Date;
import java.util.Scanner;

/**
 * @author meichenhui
 * 
 */
public class ConsoleTest {
    private static final String NO_CONSOLE      = "Error: Console unavailable";
    private static final String GREETINGS       = "Welcome to the System. Please login.%n";
    private static final String DENIED_ATTEMPT  = "Wrong user name or password [%1$d]%n";
    private static final String ACCESS_DENIED   = "Access denied%n";
    private static final String ACCESS_GRANTED  = "Access granted%n";
    private static final String UNKNOWN_COMMAND = "Unknown command [%1$s]%n";
    private static final String COMMAND_ERROR   = "Command error [%1$s]: [%2$s]%n";

    private static final String TIME_FORMAT     = "%1$tH:%1$tM:%1$tS";
    private static final String PROMPT          = TIME_FORMAT + " $ ";
    private static final String USER_PROMPT     = TIME_FORMAT + " User: ";
    private static final String PASS_PROMPT     = TIME_FORMAT
                                                        + " Password [%2$s]: ";

    private static final String USER            = "john";
    private static final String PASS            = "secret";

    public static void main(String[] args) {
        Console console = System.console();
        if (console != null) {
            if (login(console)) {
                execCommandLoop(console);
            }
        } else {
            throw new RuntimeException(NO_CONSOLE);
        }
    }

    private static boolean login(Console console) {
        console.printf(GREETINGS);

        boolean accessGranted = false;
        int attempts = 0;
        while (!accessGranted && attempts < 3) {
            String name = console.readLine(USER_PROMPT, new Date());
            char[] passdata = console.readPassword(PASS_PROMPT, new Date(),
                    name);
            if (USER.equals(name) && PASS.equals(new String(passdata))) {
                attempts = 0;
                accessGranted = true;
                break;
            }

            console.printf(DENIED_ATTEMPT, ++attempts);
        }

        if (!accessGranted) {
            console.printf(ACCESS_DENIED);
            return false;
        }

        console.printf(ACCESS_GRANTED);
        return true;
    }

    private static void execCommandLoop(final Console console) {
        while (true) {
            String commandLine = console.readLine(PROMPT, new Date());
            Scanner scanner = new Scanner(commandLine);

            if (scanner.hasNext()) {
                final String commandName = scanner.next().toUpperCase();

                try {
                    // final Command cmd = Enum.valueOf(Command.class,
                    // commandName);
                    String param = scanner.hasNext() ? scanner.next() : null;
                    // cmd.exec(console, new String[]{param}, new
                    // Command.Listener()
                    // {
                    // @Override
                    // public void exception(Exception e)
                    // {
                    // console.printf(COMMAND_ERROR, cmd, e.getMessage());
                    // }
                    // });
                    console.printf(COMMAND_ERROR, "hello");
                } catch (IllegalArgumentException e) {
                    console.printf(UNKNOWN_COMMAND, commandName);
                }
            }

            scanner.close();
        }
    }

}
